<template>
  <div>
    <introduce>
      <span class="title-gray" slot="pre">子账号/</span>
      <span class="title-bold" slot="title"> 子账号列表</span>
      <span slot="info">
        - 展示当前组织下的所有子账号，可修改子账号信息或删除子账号。</span
      >
    </introduce>
    <el-table :data="accountList" stripe style="width: 100%; margin-top: 10px">
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <el-table-column prop="account" label="账号" width="180">
      </el-table-column>
      <el-table-column prop="username" label="用户名称" width="180">
      </el-table-column>
      <el-table-column label="类型">
        <template slot-scope="scope">
          {{ scope.row.type | typename }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.cTime | dateformat }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间">
        <template slot-scope="scope">
          {{ scope.row.uTime | dateformat }}
        </template>
      </el-table-column>
      <el-table-column prop="desc" label="描述"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="edit(scope.row)"
            type="primary"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            @click="remove(scope.row)"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.currPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div> -->
    <el-dialog
      title="修改账户信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="editForm"
        :rules="rules"
        ref="editForm"
        label-width="100px"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model="editForm.account"></el-input>
        </el-form-item>
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="类型：" prop="type">
          <el-radio v-model="editForm.type" label="1"
            >超级管理员（所有权限）</el-radio
          ><br />
          <el-radio v-model="editForm.type" label="2"
            >管理员（拥有部分权限）</el-radio
          ><br />
          <el-radio v-model="editForm.type" label="3"
            >观察员（只读权限）</el-radio
          >
        </el-form-item>
        <el-form-item label="账号描述：" prop="desc"
          ><el-input
            v-model="editForm.desc"
            type="textarea"
            :rows="6"
            style="width: 220px"
          ></el-input
        ></el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Introduce from "@/components/Introduce.vue";
import { $accountList, $accountDel, $accountEdit } from "@/api/index.js";
export default {
  name: "accountList",
  components: {
    Introduce,
  },
  data() {
    return {
      accountList: [],
      total: 0,
      query: {
        currPage: 1,
        pageSize: 5,
      },
      dialogVisible: false,
      ruleForm: {
        account: "",
        username: "",
        type: 0,
      },
      editForm: {
        account: "",
        username: "",
        type: 0,
      },
      rules: {},
    };
  },
  mounted() {
    this.getAccountList();
  },
  methods: {
    handleClose() {
      this.editForm = {
        account: "",
        username: "",
        type: 0,
      };
      this.getAccountList();
      this.dialogVisible = false;
    },
    handleEdit() {
      $accountEdit(this.editForm).then((res) => {
        if (res.data.msg == "success") {
          this.dialogVisible = false;
        } else {
          this.editForm = {
            account: "",
            username: "",
            type: 0,
          };
        }
      });
      this.getAccountList();
    },
    edit(row) {
      this.editForm = row;
      this.dialogVisible = true;
    },
    getAccountList() {
      $accountList().then((res) => {
        this.accountList = res.data.data;
        this.total = res.data.total;
      });
    },
    remove(row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          $accountDel({ id: row.id }).then((res) => {
            if (res.data.msg == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getAccountList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // handleSizeChange(count) {
    //   this.query.pageSize = count;
    //   this.query.currPage = 1;
    //   this.getAccountList();
    // },
    // handleCurrentChange(page) {
    //   this.query.currPage = page;
    //   this.getAccountList();
    // },
  },
  filters: {
    dateformat(val) {
      let t = new Date(+val);
      let y = t.getFullYear();
      let m = (t.getMonth() + 1 + "").padStart(2, "0");
      let d = (t.getDate() + "").padStart(2, "0");
      return `${y}-${m}-${d}`;
    },
    typename(val) {
      if (val == 1) {
        return "超级管理员";
      } else if (val == 2) {
        return "超级管理员";
      } else if (val == 3) {
        return "观察员";
      }
      return "";
    },
  },
};
</script>

<style lang="less" scoped>
// .block {
//   text-align: center;
// }
</style>